
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Hello {
    int i=0;
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2、获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost/jdbc?characterEncoding=utf-8",
                    "root", "20080808a");
            if (conn != null) {
                System.out.println("连接成功");
            }
            //3、编写sql语句
            String sql = "select * from sys_user";

            //4、预处理sql  .var
            preparedStatement = conn.prepareStatement(sql);

            //5、如果有参数，设置参数

            //6、执行sql语句
            resultSet = preparedStatement.executeQuery();

            // 7、如果是查询就遍历结果/如果是增删改 判断是否成功
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(new StringBuilder().append(id).append("----").append(name).append("----").append(pass).toString());
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if(conn!=null){
                    conn.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();}
            }
            catch(Exception e){
                e.printStackTrace();
            }


        }
    }
}